package br.com.ufpr.sgp.pages;

import javax.ejb.EJB;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;

import br.com.sebrae.pr.sgp.entities.World;
import br.com.sebrae.pr.sgp.services.HelloWorldServiceRemote;
import br.com.ufpr.sgp.TemplatePage;
import br.com.ufpr.sgp.data.dataobjects.HelloWorld;

public class HelloWorldPage extends TemplatePage {

	private static final long serialVersionUID = -6607254469403454365L;
	
	@EJB(name="HelloWorldServiceImpl")
	private HelloWorldServiceRemote helloWorldService;
	
	public HelloWorldPage() {
		super();
		World world = helloWorldService.findWorldById(54);
		Form<HelloWorld> helloWorldForm = new Form<HelloWorld>("helloWorldForm",
				new CompoundPropertyModel<HelloWorld>(new HelloWorld()));
		
		TextField<String> textoField = (new TextField<String>("texto"));
		helloWorldForm.add(textoField.setRequired(true));

		final WebMarkupContainer wmc = new WebMarkupContainer("listContainer");

		
		ListView<HelloWorld> listView = new ListView<HelloWorld>("list") {
			
			private static final long serialVersionUID = -469290878790016836L;

			@Override
			protected void populateItem(ListItem<HelloWorld> item) {
				for(int i=0;i<10;i++)
				{
					HelloWorld world = item.getModelObject();
					world.setTexto("Texto " + i);
					item.add(new Label("helloWorldName", world.getTexto()));
				}
			}
		};
		wmc.add(listView);
		wmc.setOutputMarkupId(true);
		add(wmc);

		helloWorldForm.add(new AjaxSubmitLink("submit") {
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				HelloWorld helloWorld = (HelloWorld) form.getModelObject();
				HelloWorld newhelloWorld = new HelloWorld();
				newhelloWorld.setTexto(helloWorld.getTexto());
				target.add(wmc);
			}
		});

		add(helloWorldForm);

	}

}
